extends Panel

func _on_start_pressed():
	get_node("/root/global").level = 1
	get_node("/root/global").spell_level = 1
	change_scenes("res://Scenes/intro.tscn")

func _on_license_pressed():
	get_node("license_panel").popup()
	get_node("license_panel").get_node("button_close").grab_focus()

func _on_credits_pressed():
	change_scenes("res://Scenes/credits.tscn")

func _on_extra_pressed():
	get_node("extra_panel").get_node("instructions").set_text("Enter a secret word here!\n\n")
	get_node("extra_panel").get_node("entry_box").clear()
	get_node("extra_panel").popup()
	get_node("extra_panel").get_node("entry_box").grab_focus()

func _on_quit_pressed():
	get_tree().quit()

func _on_close_license_pressed():
	get_node("license_panel").hide()
	get_node("button_license").grab_focus()

func _on_accept_extra(entry):
	if (entry == "tabletennis"):
		print("tabletennis")
	elif (entry == "dogfight"):
		print("dogfight")
	else:
		get_node("extra_panel").get_node("instructions").set_text("Enter a secret word here!\n\nBeat the game to learn secrets!")

func _on_accept_extra_pressed():
	_on_accept_extra(get_node("extra_panel").get_node("entry_box").get_text())

func _on_close_extra_pressed():
	get_node("extra_panel").hide()
	get_node("button_extra").grab_focus()

func _ready():
	get_node("button_start").grab_focus()
	get_node("button_start").connect("pressed", self, "_on_start_pressed")
	get_node("button_license").connect("pressed", self, "_on_license_pressed")
	get_node("button_credits").connect("pressed", self, "_on_credits_pressed")
	get_node("button_extra").connect("pressed", self, "_on_extra_pressed")
	get_node("button_quit").connect("pressed", self, "_on_quit_pressed")
	get_node("license_panel").get_node("button_close").connect("pressed", self, "_on_close_license_pressed")
	get_node("extra_panel").get_node("button_close").connect("pressed", self, "_on_close_extra_pressed")
	get_node("extra_panel").get_node("button_accept").connect("pressed", self, "_on_accept_extra_pressed")
	get_node("extra_panel").get_node("entry_box").connect("text_entered", self, "_on_accept_extra")
	var f = File.new()
	var err = f.open("res://gpl.txt", File.READ)
	var gpl_as_text = f.get_as_text()
	f.close()
	get_node("license_panel").get_node("licenses").get_node("Game License").push_align(0)
	get_node("license_panel").get_node("licenses").get_node("Game License").add_text(gpl_as_text)
	f = File.new()
	err = f.open("res://apache.txt", File.READ)
	var apache_as_text = f.get_as_text()
	get_node("license_panel").get_node("licenses").get_node("Fonts License").push_align(0)
	get_node("license_panel").get_node("licenses").get_node("Fonts License").add_text(apache_as_text)
	get_node("music").set_stream(load("res://Music/mainmenu.ogg"))
	get_node("music").play()

func change_scenes(next_scene):
	get_node("music").stop()
	get_node("music").queue_free()
	get_node("/root/global").goto_scene(next_scene)
